package cn.findviewbydream.evil.bigtoutiao.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.lzy.okgo.OkGo;
import com.yuyh.easyadapter.recyclerview.EasyRVAdapter;
import com.yuyh.easyadapter.recyclerview.EasyRVHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.findviewbydream.evil.bigtoutiao.R;
import cn.findviewbydream.evil.bigtoutiao.base.BaseFragment;
import cn.findviewbydream.evil.bigtoutiao.base.IP;
import cn.findviewbydream.evil.bigtoutiao.dto.Result;
import cn.findviewbydream.evil.bigtoutiao.entity.NewsEntity;
import cn.findviewbydream.evil.bigtoutiao.utils.GsonCallBack;
import okhttp3.Call;
import okhttp3.Response;

/********************************************
 * 工程名：BigTouTiao
 * 包名：cn.findviewbydream.evil.bigtoutiao.fragment
 * 时间：17-7-1
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public class IndexFragment extends BaseFragment
{
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.news_recyclerView)
    LRecyclerView newsRecyclerView;
    Unbinder unbinder;

    private static IndexFragment fragment;
    private List<NewsEntity> newsList;
    private LRecyclerViewAdapter adapter;

    public static IndexFragment getInstance()
    {
        if (fragment == null)
            fragment = new IndexFragment();
        return fragment;
    }

    @Override
    public int setFragmentViewId()
    {
        return R.layout.fragment_index;
    }

    @Override
    public void setFragmentCreateView()
    {
        initView();
    }

    @Override
    protected void initView()
    {
        initRecyclerView();
        loadData();
    }

    private void initRecyclerView()
    {
        newsList = new ArrayList<>();

        newsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new LRecyclerViewAdapter(new Adapter());
        newsRecyclerView.setAdapter(adapter);
    }

    private void loadData()
    {
        OkGo.get(IP.IP + "/news/list/" + newsList.size()).execute(new GsonCallBack<Result<List<NewsEntity>>>()
        {
            @Override
            public void onSuccess(Result<List<NewsEntity>> result, Call call, Response response)
            {
                if (result.getCode() == 1)
                {
                    newsList.addAll(result.getData());
                    adapter.notifyDataSetChanged();
                }
                else
                    Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
            }
        });
    }

    class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view;

            switch (viewType)
            {
                case 0:
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_news_1, parent, false);
                    return new ViewHolder1(view);
                case 1:
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_news_2, parent, false);
                    return new ViewHolder2(view);
                case 2:
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_news_3, parent, false);
                    return new ViewHolder3(view);
                case 3:
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_news_video, parent, false);
                    return new ViewHolder1(view);
            }

            return null;
        }

        @Override
        public int getItemViewType(int position)
        {
            NewsEntity news = newsList.get(position);

            if (news.getImgs() == null)
                return 0;
            else
            {
                if (news.getImgs().length == 2)
                    return 1;
                else if (news.getImgs().length >= 3)
                    return 2;
            }

            return 0;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            if (holder instanceof ViewHolder1)
            {
                NewsEntity news = newsList.get(position);
                ((ViewHolder1) holder).titleText.setText(news.getTitle());
                Glide.with(getActivity()).load(news.getImg()).into(((ViewHolder1) holder).img);
                ((ViewHolder1) holder).authorText.setText(news.getTag());
            }
            else if (holder instanceof ViewHolder2)
            {
                NewsEntity news = newsList.get(position);
                ((ViewHolder2) holder).titleText.setText(news.getTitle());
                Glide.with(getActivity()).load(news.getImgs()[0]).into(((ViewHolder2) holder).img1);
                Glide.with(getActivity()).load(news.getImgs()[1]).into(((ViewHolder2) holder).img2);
                ((ViewHolder2) holder).authorText.setText(news.getTag());
            }
            else if (holder instanceof ViewHolder3)
            {
                NewsEntity news = newsList.get(position);
                ((ViewHolder3) holder).titleText.setText(news.getTitle());
                Glide.with(getActivity()).load(news.getImgs()[0]).into(((ViewHolder3) holder).img1);
                Glide.with(getActivity()).load(news.getImgs()[1]).into(((ViewHolder3) holder).img2);
                Glide.with(getActivity()).load(news.getImgs()[2]).into(((ViewHolder3) holder).img3);
                ((ViewHolder3) holder).authorText.setText(news.getTag());
            }
        }

        @Override
        public int getItemCount()
        {
            return newsList.size();
        }

        class ViewHolder1 extends RecyclerView.ViewHolder
        {
            @BindView(R.id.title_text)
            TextView titleText;
            @BindView(R.id.img)
            ImageView img;
            @BindView(R.id.author_text)
            TextView authorText;
            @BindView(R.id.close)
            ImageView close;


            ViewHolder1(View view)
            {
                super(view);
                ButterKnife.bind(this, view);
            }
        }

        class ViewHolder2 extends RecyclerView.ViewHolder
        {
            @BindView(R.id.title_text)
            TextView titleText;
            @BindView(R.id.author_text)
            TextView authorText;
            @BindView(R.id.close)
            ImageView close;
            @BindView(R.id.img1)
            ImageView img1;
            @BindView(R.id.img2)
            ImageView img2;


            ViewHolder2(View view)
            {
                super(view);
                ButterKnife.bind(this, view);
            }
        }

        class ViewHolder3 extends RecyclerView.ViewHolder
        {
            @BindView(R.id.title_text)
            TextView titleText;
            @BindView(R.id.author_text)
            TextView authorText;
            @BindView(R.id.close)
            ImageView close;
            @BindView(R.id.img1)
            ImageView img1;
            @BindView(R.id.img2)
            ImageView img2;
            @BindView(R.id.img3)
            ImageView img3;

            ViewHolder3(View view)
            {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
